<?php

namespace app\admin\controller;


use app\admin\library\Auth;
use app\common\library\traits\Jump;
use brand\logic\BrandHelper;
use brand\traits\BrandAdmin;
use think\App;
use think\exception\ValidateException;
use think\facade\View;

class Base
{
    use Jump;
    use BrandAdmin;

    /**
     * 权限控制类.
     * @var Auth
     */
    protected $auth = null;

    /**
     * @var bool 验证失败是否抛出异常
     */
    protected $failException = false;

    /**
     * @var bool 是否批量验证
     */
    protected $batchValidate = false;


    /**
     * 无需登录的方法,同时也就不需要鉴权了.
     * @var array
     */
    protected $noNeedLogin = [];

    /**
     * 无需鉴权的方法,但需要登录.
     * @var array
     */
    protected $noNeedRight = [];



    /**
     * Request实例
     * @var \think\Request
     */
    protected $request;

    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;



    public function __construct(App $app)
    {


        $this->app = $app;
        $this->request = is_null($app->request) ? \think\facade\Request::instance() : $app->request;

        $this->_initialize();
        //初始化CURD
        $this->init_curd();
    }

    protected function _initialize()
    {
        $path = request()->pathinfo();
        $this->auth = Auth::instance();
        $actionname = strtolower($this->request->action());

        // 设置当前请求的URI
        $this->auth->setRequestUri($path);
        // 检测是否需要验证登录
        if (! $this->auth->match($this->noNeedLogin)) {
            //检测是否登录
            if (! $this->auth->isLogin()) {
                $this->redirect(url('/'.BrandHelper::getAdminName().'/login/index')->build());
            }
            // 判断是否需要验证权限
            if (($actionname != 'index' && $actionname != 'profile') && ! $this->auth->match($this->noNeedRight)) {
                // 判断控制器和方法判断是否有对应权限

                if (! $this->auth->check($path)) {
                    $this->error('您无权访问');
                }
            }
        }


        View::assign('root',request()->root());




    }


    /**
     * 验证数据.
     *
     * @param array        $data     数据
     * @param string|array $validate 验证器名或者验证规则数组
     * @param array        $message  提示信息
     * @param bool         $batch    是否批量验证
     * @param mixed        $callback 回调方法（闭包）
     *
     * @throws ValidateException
     * @return array|string|true
     */
    protected function validate($data, $validate, $message = [], $batch = false, $callback = null)
    {

        if (is_array($validate)) {
            $v = validate();
            $v->rule($validate);
        } else {
            // 支持场景
            if (strpos($validate, '.')) {
                [$validate, $scene] = explode('.', $validate);
            }

            $v = validate($validate);

            ! empty($scene) && $v->scene($scene);
        }

        // 批量验证
        if ($batch || $this->batchValidate) {
            $v->batch(true);
        }
        $v->failException(false);
        // 设置错误信息
        if (is_array($message)) {
            $v->message($message);
        }
        // 使用回调验证
        if ($callback && is_callable($callback)) {
            call_user_func_array($callback, [$v, &$data]);
        }

        if (! $v->check($data)) {
            if ($this->failException) {
                throw new ValidateException($v->getError());
            }
            return $v->getError();
        }

        return true;
    }







}